<template>
  <div class="SysLogin-container">
    <div class="box">
      <el-form
          ref="loginForm"
          class="login-form"
          :model="loginData"
          :rules="loginRules"
        >
          <h2 style="color: white;text-align: center;margin-bottom: 100px;">后台管理系统-登录</h2>

          <el-form-item prop="username">
            <el-input
              size="large"
              placeholder="用户名"
              v-model="loginData.username"
            ></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              size="large"
              type="password"
              placeholder="密码"
              v-model="loginData.password"
              show-password
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="width: 500px;" size="large" @click="handleLogin"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
    </div>
  </div>
</template>

<script>
import {toRefs,reactive} from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { SysLoginApi } from '@/api/system/sysuser'
export default {
  setup() {
    const store = useStore()
    const router = useRouter()
    const data = reactive({
      loginData: {
        username: '',
        password: ''
      }
    })
const loginRules = {
      username: [{ required: true, message: '请输入用户名', trigger: 'blur' },
                 {min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                ],
      password: [{ required: true, message: '请输入密码', trigger: 'blur' },
                 {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                ]
    }
    const handleLogin = () => {
      SysLoginApi(data.loginData).then((res) => {
        if (res.data) {
          store.commit('setSysUserInfo', res.data)
          localStorage.setItem('SysLoginData', JSON.stringify(res.data))
          router.push({
            path: '/admin/home'
          })
        }
      })
    }
    return { ...toRefs(data), handleLogin, loginRules, router }
  },
  methods: {}
}
</script>

<style>
.SysLogin-container{
  background-image: url('../../assets/loginBackground.png');
  background-repeat: no-repeat;
  overflow: hidden;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}
.box{
  width: 500px;
  height: 100vh;
  margin-top: 100px;
}
.el-input__wrapper{
  background-color: rgba(0, 0, 0, 0);
}
.el-input__inner{
  color: #fff;
}
.el-input .el-input__clear, .el-input .el-input__password{
  color: #fff;
}
</style>
